﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using OBS;
using OBS.Model;

namespace FreeAccessNetdisk.src.classes
{
    public class CloudClient
    {
        private ObsClient _client;
        private ObsConfig _config;
        //private ObsBucket userBucket;
        private string userBucket;//这里先用桶名;
        private UniversalLogger _logger;

        //官方给了ObsConfig类,但这里因为还是测试所以用的是自己的，明文存储了;


        public CloudClient(string bucketName, UniversalLogger logger)
        {
            _config = new ObsConfig();
            _client = new ObsClient(_config.AccessKey, _config.SecretKey, _config.Endpoint);
            userBucket = bucketName;//这里选择桶名
            _logger = logger;
        }

        public void SetLogger(UniversalLogger logger)
        {
            _logger = logger;
        }

        public void PutObject(string objectname, string localfile)
        {
            try
            {
                PutObjectRequest request = new PutObjectRequest()
                {
                    BucketName = userBucket,  //目标桶名
                    ObjectKey = objectname,   //待传入对象名(对象名是对象在桶中的完整路径，如folder/test.txt，路径中不包含桶名)
                    FilePath = localfile,//待上传的本地文件路径，需要指定到具体的文件名
                };
                PutObjectResponse response = _client.PutObject(request);
                _logger.Log(LogType.CLOUD, "成功上传");
            }
            catch (ObsException ex)
            {
                _logger.Log(LogType.CLOUD, "上传失败");
                _logger.Log(LogType.CLOUD, ex.ErrorMessage);
            }
        }

        public void GetObject(string objectname,string savepath)
        {
            try
            {
                GetObjectRequest request = new GetObjectRequest()
                {
                    BucketName = userBucket,
                    ObjectKey = objectname,
                };
                using (GetObjectResponse response = _client.GetObject(request))
                {
                    string dest = savepath;

                    // 使用 FileMode.Create 参数确保如果文件已存在则覆盖
                    using (var fileStream = new FileStream(dest, FileMode.Create, FileAccess.Write))
                    {
                        response.OutputStream.CopyTo(fileStream);
                    }
                    _logger.Log(LogType.CLOUD, "成功下载");
                }

            }
            catch (ObsException ex)
            {
                _logger.Log(LogType.CLOUD, "下载失败");
                _logger.Log(LogType.CLOUD, ex.ErrorMessage);
            }

        }

        public void DeleteObject(string objectname)
        {
            try
            {
                DeleteObjectRequest request = new DeleteObjectRequest()
                {
                    BucketName = userBucket,
                    ObjectKey = objectname,
                };
                DeleteObjectResponse response = _client.DeleteObject(request);
                _logger.Log(LogType.CLOUD, "删除成功");
            }
            catch (ObsException ex)
            {
                _logger.Log(LogType.CLOUD, "删除失败");
                _logger.Log(LogType.CLOUD, ex.ErrorMessage);
            }
        }



        //获取桶中对象列表
        public List<string> GetObjects()
        {
            try
            {   
                List<string> objects = new List<string>();
                ListObjectsRequest request = new ListObjectsRequest();
                request.BucketName = userBucket;
                ListObjectsResponse response = _client.ListObjects(request);//桶中用“/”来模拟文件夹
                foreach (ObsObject entry in response.ObsObjects)
                {
                    string objectDetails = string.Format("key = {0}, LastModified = {1}", entry.ObjectKey, entry.LastModified);
                    objects.Add(objectDetails);
                }
                _logger.Log(LogType.CLOUD, "获取桶中文件成功");
                return objects;
            }
            catch (ObsException ex)
            {
                _logger.Log(LogType.CLOUD, "失败");
                _logger.Log(LogType.CLOUD, ex.ErrorMessage);
                return null;
            }
        }



    }
}
